//
//  EmotionPageView.m
//  微博2期
//
//  Created by J on 15/12/15.
//
//

#import "EmotionPageView.h"
#import "Emotion.h"//模型头文件
#import "EmotionPopView.h"//点击放大
#import "EmotionButton.h"
#import "EmotionTool.h"//存储表情
@interface EmotionPageView()
/**
 *  点击表情按钮后弹出的放大镜
 */
@property(nonatomic,strong)EmotionPopView * popView;
@property(nonatomic,strong)UIButton* deleteButton;

@end

@implementation EmotionPageView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        //1.删除按钮
        UIButton* deleteButton = [[UIButton alloc]init];
        [deleteButton setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:UIControlStateNormal];
        [deleteButton setImage:[UIImage imageNamed:@"compose_emotion_delete_highlighted"] forState:UIControlStateHighlighted];
        [deleteButton addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
        self.deleteButton = deleteButton;
        [self addSubview:deleteButton];
        
        //2.添加长按手势
        [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]];
    }
    
    return self;
}

//找出手指位置所在表情按钮
- (EmotionButton*)emotionButtonWithLocation:(CGPoint)location{
    
    //获得手指所在的表情按钮

    NSUInteger count =self.emotions.count;
    for (int i=0; i<count; i++) {
        
        //设置表情数据
        EmotionButton* btn = self.subviews[i+1];
        if (CGRectContainsPoint(btn.frame, location)) {
            //已经找到手指所在的表情按钮 退出循环
            return btn;
        }
    }
        return nil;

}
//在这个方法中处理长按手势
- (void)longPress:(UILongPressGestureRecognizer*)longPress{
    //获得手指所在的表情按钮
    CGPoint location = [longPress locationInView:longPress.view];
    EmotionButton* btn = [self emotionButtonWithLocation:location];

    switch (longPress.state) {
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:{//触摸事件被打断。很重要
            NSLog(@"UIGestureRecognizerStateCancelled/UIGestureRecognizerStateEnded");
            [self.popView removeFromSuperview];
            //如果手指还在表情按钮上
            if (btn) {
                //发出通知
                [self selectEmotion:btn.emotion];

            }
            //手指已经不再触摸pageView
        }break;
        case UIGestureRecognizerStateBegan://刚检测到手势开始
        case UIGestureRecognizerStateChanged://手指的位置改变
        {
            [self.popView showFrom:btn];

            //手指已经不再触摸pageView
    }
            break;

            
        default:
            break;
    }
    
//    NSLog(@"longPress");

    
    
}


//** 监听删除按钮点击*/
- (void)deleteClick:(UIButton*)button{
    [NoficationCenter postNotificationName:EmotionDidDeleteNotification object:nil userInfo:nil ];
}

- (EmotionPopView *)popView{
    if (!_popView) {
        self.popView = [EmotionPopView popView];
    }
    return _popView;
}


//设置表情
- (void)setEmotions:(NSArray *)emotions{
    
    _emotions = emotions;
//    NSLog(@"%zd",emotions.count);
    NSUInteger count = emotions.count;
    for (int i=0; i<count; i++) {
        
        //设置表情数据
        EmotionButton* btn = [[EmotionButton alloc]init];
        btn.tag = i;
//        [btn setBackgroundColor:RamdomColor];
         btn.emotion = emotions[i];
        //emotion.code 十六进制 ->Emoji字符
        [self addSubview:btn];
        //监听按钮点击
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    }
    
}
//CUICatalog: Invalid asset name supplied: 警告原因 尝试加载的图片不存在

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat inset = 10;//四周内边距
    CGFloat btnW = (self.width - 2*inset)/EmotionMaxCols;
    CGFloat btnH = (self.height - inset)/EmotionMaxRows;
    NSUInteger maxCount = EmotionMaxCols;
    NSUInteger count = self.emotions.count;
    for (int i=0; i<count; i++) {
        UIButton* btn = self.subviews[i+1];//跳过0 按钮 因为删除按钮最先被添加第0个子视图
        btn.x =inset + btnW * (i%maxCount);
        btn.y = inset + btnH*(i/maxCount);
        btn.width = btnW ;
        btn.height = btnH;
    }

    //设置删除按钮
    
    self.deleteButton.width = btnW;
    self.deleteButton.height = btnH;
    self.deleteButton.y = self.height - btnH;
    self.deleteButton.x = self.width - inset - btnW;//宽度减去间距和一个按钮的宽度
    
    
}

//监听表情按钮点击
- (void)btnClick:(EmotionButton*)btn{
    
//显示popView
    [self.popView showFrom:btn];
   
    //等会儿让popView自动消失
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.popView removeFromSuperview];
        
    });
    
    [self selectEmotion:btn.emotion];
}


//选中某个表情发出通知
- (void)selectEmotion:(Emotion*)emotion{
    //将这个表情存进沙盒
    
    [EmotionTool addRecentEmotion:emotion];
    //发出通知
    NSMutableDictionary * userInfo =[NSMutableDictionary dictionary];
    userInfo[SelectEmotionKey] = emotion;
    [NoficationCenter postNotificationName:EmotionDidSelectNotification object:nil userInfo:userInfo];
}
@end
